﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CLFramework.Infrastructure.Validation
{

    public class ModelResult
    {
        public ModelResult()
            : this(null)
        {
        }

        public ModelResult(ValidationStateDictionary validationState)
        {
            ValidationState = validationState;
            IsValid = validationState == null || validationState.IsValid;
        }

        public ValidationStateDictionary ValidationState { get; protected set; }
        public bool IsValid { get; protected set; }

        public Exception GetFirstException()
        {
            if (!IsValid)
                foreach (KeyValuePair<Type, ValidationState> item in ValidationState)
                    if (item.Value.Errors.Count > 0)
                        return item.Value.Errors[0].Exception;

            return null;
        }
    }

}
